package org.jaaksi.view.pop;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

/**
 * Created by Fcy on 2016/2/3.<br/>
 * 建议使用不获取焦点的时候，采用popupwindow, 获取焦点的使用dialog
 *
 * @deprecated {@link LoadingTip}
 */
public class LoadingBar extends PopupWindow {
    private Activity activity;

    private static final int SHOW = 1;

    // 该handler必须在主线程中，即LoadingBar必须在主线程中创建
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == SHOW) {
                showLoading((View) msg.obj);
            }
        }
    };

    public LoadingBar(Context context) {
        this(context, false);

    }

    public LoadingBar(Context context, boolean isFocusable) {
        this(context, null, isFocusable);
    }

    public LoadingBar(Context context, View view, boolean isFocusable) {
        super(WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT);
        activity = (Activity) context;
        if (view == null)
            view = LayoutInflater.from(context).inflate(R.layout.lib_loading,
                null);
        view.setVisibility(View.VISIBLE);
        // 如果设置了聚焦，如果没有设置setBackgroundDrawable将无法响应返回键
        // 如果设置了，将由PopupWindow响应返回键，Activity无法响应，且点击PopupWindow外部消失
        setFocusable(isFocusable);
        // 是否点击外部消失，必须设置setBackgroundDrawable,否则无效
        // setOutsideTouchable(false);

        // 如果设置了setBackgroundDrawable且设置聚焦，
        // 则setOutsideTouchable无论是true还是false都会消失
        // setBackgroundDrawable(new ColorDrawable());
        setContentView(view);
    }

    public Handler getHandler() {
        return mHandler;
    }

    /**
     * 使用DecorView
     */
    public void show() {
        show(activity.getWindow().getDecorView());
    }

    /**
     * @param parent 找到这个view说Attach的window
     * @see Handler#dispatchMessage(Message)
     */
    public void show(final View parent) {
        if (parent == null) {
            throw new IllegalArgumentException("parent can not be null");
        }

        if (parent.getWindowToken() != null) {
            showLoading(parent);
        } else {
            mHandler.post(new Runnable() {

                @Override
                public void run() {
                    sendShowMsg(parent);
                }
            });
        }
    }

    /**
     * 需要post的由Handler发送msg，当执行到时，messageQueue中就会has 这个msg,这样cancel就可以处理
     *
     * @param parent
     * @see #cancel()
     */
    private void sendShowMsg(View parent) {
        Message msg = mHandler.obtainMessage(SHOW);
        msg.obj = parent;
        mHandler.sendMessage(msg);
    }

    /**
     * 关闭，不要调用dismiss()方法
     */
    public void cancel() {
        if (isShowing()) {
            // 如果在showing证明不是handler发送的或者已经发送了，这时候MessageQueue中都已经没有SHOW这个msg了，所以不需要移除
            dismiss();
        } else {
            mHandler.removeMessages(SHOW);
        }
        // mHandler.removeMessages(SHOW);
    }

    public void onDestroy() {
        cancel();
        mHandler = null;
        activity = null;
    }

    /**
     * @param parentView
     * @nonNull
     */
    private void showLoading(View parentView) {
        showAtLocation(parentView, Gravity.CENTER, 0, 0);
    }

}
